package cn.zhf.shop.controller;

import cn.zhf.shop.entity.ConsoleUser;
import cn.zhf.shop.entity.Message;
import cn.zhf.shop.entity.lesson.Lesson;
import cn.zhf.shop.entity.search.CreditApplySearch;
import cn.zhf.shop.entity.search.LessonSearch;
import cn.zhf.shop.service.CampusService;
import cn.zhf.shop.service.LessonService;
import cn.zhf.shop.service.LessonTypeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;

@RestController
@RequestMapping("/api/lesson")
@Api(tags = "课程管理")
public class LessonController {
    @Resource
    private LessonService lessonService;

    @Resource
    private LessonTypeService lessonTypeService;

    @Resource
    private CampusService campusService;

    @GetMapping("/all")
    @ApiOperation(value = "获得课程列表")
    public Message<?> getAllLessonForOverview(){

        List<Lesson> lessonList =lessonService.selectAll2();
        List<Lesson> lessonList1 = new LinkedList<>();
        for(int i=0;i<lessonList.size();i++)
        {
            Lesson lesson = lessonList.get(i);
            lesson.setCampusName(campusService.selectCampusNameByCampusId(lesson.getCampus()));
            int a= Integer.parseInt(lesson.getLessonType());
            lesson.setTypeName(lessonTypeService.selectTypeNameById(a));
            lessonList1.add(lesson);
        }
        return Message.success(null).add(lessonList1);
    }

    @GetMapping("/")
    @ApiOperation(value = "根据id获得课程列表")
    public Message<Lesson> getAllLessonById(int id){
        return lessonService.selectByPrimaryKey(id);
    }

    @GetMapping("/district")
    @ApiOperation(value = "根据区县名获得课程列表")
    public Message<List<Lesson>> getAllLessonByDistrict(String district){
        return lessonService.selectOverviewByDistrict(district);
    }

    @GetMapping("/campus")
    @ApiOperation(value = "根据校区id获得课程列表")
    public Message<List<Lesson>> getAllLessonByCampus(String campusName){
        return lessonService.selectOverviewByCampus(campusName);
    }

    @GetMapping("/head")
    @ApiOperation(value = "根据校区id获得课程列表")
    public Message<List<Lesson>> getByHead(int head){

        List<Lesson> lessonList= lessonService.selectByHead2(head);
        List<Lesson> lessonList1 = new LinkedList<>();
        for(int i=0;i<lessonList.size();i++)
        {
            Lesson lesson = lessonList.get(i);
            lesson.setCampusName(campusService.selectCampusNameByCampusId(lesson.getCampus()));
            int a= Integer.parseInt(lesson.getLessonType());
            lesson.setTypeName(lessonTypeService.selectTypeNameById(a));
            lessonList1.add(lesson);
        }
        return Message.success(null).add(lessonList1);
    }

    @GetMapping("/type")
    @ApiOperation(value = "根据课程类型id获得课程列表")
    public Message<List<Lesson>> getAllLessonByType(String type){
        return lessonService.selectOverviewByType("%"+type+"%");
    }

    @GetMapping("/name")
    @ApiOperation(value = "根据课程名称获得课程列表")
    public Message<List<Lesson>> getAllLessonByName(String name){
        return lessonService.selectOverviewByName(name);
    }

    @PostMapping("/append")
    @ApiOperation(value = "添加课程")
    public Message appendLesson(Lesson lesson){
        lesson.setPostTime(new Date());
        return lessonService.insert(lesson);
    }


    @PostMapping("/update")
    @ApiOperation(value = "更新课程")
    public Message updateLesson(Lesson lesson){
        Date date = new Date();
        lesson.setPostTime(date);
        return lessonService.updateByPrimaryKey(lesson);
    }

    @PostMapping("/remove")
    @ApiOperation(value = "删除课程")
    public Message removeLesson(int id){
        return lessonService.deleteByPrimaryKey(id);
    }

    @ApiOperation("模糊搜索")
    @PostMapping("/mohu/search")
    public Message<List<Lesson>> mohusearch(String str,Integer userId){
        return lessonService.selectByLessonIdAndLessonNameAndCampusAndLessonType(str,userId);
    }

    @ApiOperation("条件搜索")
    @PostMapping("/confident/search")
    public Message<List<Lesson>> selectByUserIdAndOrgIdAndUserGroupAndUsername2(Integer lessonId,String lessonName,Integer campus,String lessonType,Integer userId){
        return lessonService.selectByLessonIdAndLessonNameAndCampusAndLessonType2(lessonId, lessonName, campus, lessonType,userId);
    }

    @ApiOperation(value = "多条件组合模糊查询")
    @PostMapping("/mohu/condition/search")
    public Message mohuConditionSearch(LessonSearch c){

        return lessonService.mohuConditionSearch(c);
    }

    @ApiOperation(value = "根据机构id获得旗下所有服务项目")
    @GetMapping(value = "/all/orgId")
    public Message getAllByOrgId(int orgId){
        return lessonService.getAllByOrgId(orgId);
    }

}
